import axios from '@/api/axios'
import qs from 'qs'
import {publicUrl, baseUrl2, fytestUrl, payrollUrl, appUrl, clientId, clientSecret} from '@/config/config'
import {homeWorkUrl,schoolUrl,authUrl,organizationUrl, fileapiUrl} from '@/config/config'
import   cookie from '@/util/cookie'
import Vue from 'vue'
import store from '@/store'



export const deptListByTeacherId = (params) => {
    return new Promise((resolve, reject) => {
      axios({
          url: organizationUrl + '/api/Organization/DeptListByTeacherId',
          method: 'get',
          params
      }).then(res => {
          resolve(res.data)
      }).catch(error => {
          // location.href = 'http://www.xueerqin.net'
          reject(error)
      })
    })
  }

export const getAccessToken = () => {
    return new Promise((resolve,reject) => {
        axios({
            url: fytestUrl + '/api/CommonApi/GetTokenByCache',
        }).then(res => {
            resolve(res.data)
        })
    });
}

export const getOpenid = (params) => {
    return new Promise((resolve,reject) => {
        axios({
            url: fytestUrl + "/api/OAuthApi/GetAccessToken",
            params
        }).then(res => {
            resolve(res.data)
        })
    });
}

export const unionid = (params) => {
    return new Promise((resolve,reject) => {
        axios({
            url: fytestUrl + "/api/OAuthApi/GetUnionID",
            params
        }).then(res => {
            resolve(res.data)
        })
    });
}

export const GetConfig = () => {
    // console.log(params)
    return new Promise((resolve,reject) => {
        var url = window.location.href
        axios({
            url: fytestUrl + '/api/CommonApi/GetConfig?url=' + url,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        })
    });
}

// 微信上传图片
export const uploadWechatImg = (data) => {
    // console.log(data)
    // debugger
    return new Promise((resolve,reject) => {
        axios({
            url: fileapiUrl + '/api/WechatFile/UploadClassZoneFile',
            method: 'post',
            data
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject()
        })
    });
}

// 根据老师id查询年级班级
export const getTeacher = (teacherId) => {
    return new Promise((resolve, reject) => {
        axios({
            url: publicUrl + '/api/Data/GetClassRoomTeacherByTeacherId?data={teacherId:"' + teacherId + '"}',
            method: 'get'
        }).then(res => {
          resolve(res.data)
        })
    })
  }

  // 获取登录token 信息存入Cookies和catcher
export const getTokenByMoblie = () => {
    return new Promise((resolve, reject) => {
        axios({
            url: appUrl + '/bdc/api/app/users/tokenByMoblie',
            method: 'get',
        }).then(res => {
            resolve(res.data)
        })
    })
}

//获取链接信息
export const getLinkInfo = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: homeWorkUrl + '/api/HomeWorkMoblie/GetHtmlModel',
            method: 'get',
            params,
            timeout:3000
        }).then(res => {
            resolve(res.data)
        }).catch(res => {
            resolve(res.data)
        })
    })
}
//获取学校下的年级
export const getGrade = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: schoolUrl + '/api/app/grade',
            method: 'get',
            params
        }).then(res => {
            resolve(res.data)
        }).catch(res => {
            resolve(res.data)
        })
    })
}
//获取学校下的年级下得班级
export const getClass = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: schoolUrl + '/api/app/class',
            method: 'get',
            params
        }).then(res => {
            resolve(res.data)
        }).catch(res => {
            resolve(res.data)
        })
    })
}

export const getManListV2 = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: payrollUrl + '/api/TeachSalary/GetTeachDeptV2',
            method: 'get',
            params
        }).then(res => {
            resolve(res.data)
        })
    })
}

export const getRoleList = (params) => {
    return new Promise((resolve, reject) => {
      axios({
        url: appUrl + '/bdc/api/app/main/identityOptions',
        method: 'get'
      }).then(res => {
        resolve(res.data)
      })
    })
  }

export const refreshToken = (params) => {
    params.clientId = clientId
    params.clientSecret = clientSecret

    return new Promise((resolve, reject) => {
        axios({
            url: authUrl + '/api/auth/login/refresh-token',
            method: 'post',
            // withCredentials: true,
            data: params
        }).then(res => {
            resolve(res.data)
        }).catch(
            error => {
            var data = error.response.data
            Vue.$vux.alert.show({
                content: data.message,
                maskZIndex: 100,
                onHide () {
                    store.dispatch('setting/loginOut')
                }
            })
          }
        )
    })
  }

  export const getTeacherByLoginUserId = (id) => {
    return new Promise((resolve, reject) => {
      axios({
        url: appUrl + '/bdc/api/app/users/' + id + '/teacher',
        method: 'get'
      }).then(res => {
        resolve(res.data)
      }).catch(err => {
        reject(err)
      })
    })
  }
